<html>
<head><meta charset="utf-8"><title>Taking mutable references via programmatic operation · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html">Taking mutable references via programmatic operation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182596071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596071">(Dec 04 2019 at 20:35)</a>:</h4>
<p>I had an idea yesterday for an operation that I think makes sense and would be sound, but isn't currently possible in Rust. I'm trying to figure out the exact semantics and whether I'm missing anything that would make it unsound.</p>



<a name="182596101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596101">(Dec 04 2019 at 20:35)</a>:</h4>
<p>How mutable references work today:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="c1">// cannot touch x here</span>
<span class="w">        </span><span class="o">*</span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// prints 20</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="182596131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596131">(Dec 04 2019 at 20:35)</a>:</h4>
<p>But you can't, today, write a <em>function</em> with the same semantics as the operation <code>&amp;mut x</code>.</p>



<a name="182596186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596186">(Dec 04 2019 at 20:36)</a>:</h4>
<p>Hypothetical:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">get_mut_ref</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="c1">// cannot touch x here.</span>
<span class="w">        </span><span class="o">*</span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// prints 20</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="182596271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596271">(Dec 04 2019 at 20:37)</a>:</h4>
<p>The semantics I'm looking for would also allow writing a function that could return a mutable reference of a <em>different</em> type that was still tied to the owned value it mutably-referenced.</p>



<a name="182596352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596352">(Dec 04 2019 at 20:38)</a>:</h4>
<p>I'm a bit confused -- isn't this already true of all methods that take <code>&amp;mut self</code> and return <code>&amp;mut U</code>?</p>



<a name="182596359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596359">(Dec 04 2019 at 20:38)</a>:</h4>
<p>For instance:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">4</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">get_mut_ref_to_bytes</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="c1">// cannot touch x here.</span>
<span class="w">        </span><span class="n">y</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="c1">// assuming little-endian for the sake of example</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"> </span><span class="c1">// prints 20</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="182596614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596614">(Dec 04 2019 at 20:41)</a>:</h4>
<p>(e.g., for your last example, <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0aebe3e2d489b2a645b836f2d9b49a1e" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0aebe3e2d489b2a645b836f2d9b49a1e">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=0aebe3e2d489b2a645b836f2d9b49a1e</a>)</p>



<a name="182596658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596658">(Dec 04 2019 at 20:41)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> Wait, that's legal?</p>



<a name="182596679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596679">(Dec 04 2019 at 20:41)</a>:</h4>
<p>You can consume <code>&amp;mut self</code> and return <code>&amp;mut OtherType</code>?</p>



<a name="182596749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596749">(Dec 04 2019 at 20:42)</a>:</h4>
<p>My understanding is yes, for sure. (Indeed, this is a frequent <em>pain</em> point)</p>



<a name="182596753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596753">(Dec 04 2019 at 20:42)</a>:</h4>
<p>Do the lifetime semantics actually work just like you have a <code>&amp;mut</code> referencing that owned type?</p>



<a name="182596775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596775">(Dec 04 2019 at 20:42)</a>:</h4>
<p>the lifetime of both <code>&amp;mut</code> is the same</p>



<a name="182596805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596805">(Dec 04 2019 at 20:42)</a>:</h4>
<p>And you <em>can't</em> mutably reference the original type anymore, because that would alias? Because it seems like the <code>transmute</code> you show produces aliased <code>&amp;mut</code> values which would be UB...</p>



<a name="182596846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596846">(Dec 04 2019 at 20:43)</a>:</h4>
<p>it does not, because it consumes the previous <code>&amp;mut</code> (or so I understand it)</p>



<a name="182596864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596864">(Dec 04 2019 at 20:43)</a>:</h4>
<p>I didn't know you could "consume" a <code>&amp;mut</code>.</p>



<a name="182596900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596900">(Dec 04 2019 at 20:43)</a>:</h4>
<p>That's useful, and if that's the semantic, that's incredibly helpful. Is there somewhere I could read more about that semantic and verify?</p>



<a name="182596911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596911">(Dec 04 2019 at 20:43)</a>:</h4>
<p>I'm a bit confused, I guess</p>



<a name="182596915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596915">(Dec 04 2019 at 20:43)</a>:</h4>
<p>Damn, that's a pretty cool way to have bit/byte fiddling.</p>



<a name="182596919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596919">(Dec 04 2019 at 20:43)</a>:</h4>
<p>Like, which semantic is this?</p>



<a name="182596926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596926">(Dec 04 2019 at 20:43)</a>:</h4>
<p>On a basic level, <code>&amp;mut T</code> does not implement Copy, so it is moved. Reborrows make that slightly complicated though.</p>



<a name="182596931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182596931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182596931">(Dec 04 2019 at 20:43)</a>:</h4>
<p>This is in the context of the "safe transmute" proposal.</p>



<a name="182597054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597054">(Dec 04 2019 at 20:44)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> The standard mutable reference semantic. "You can only take a mutable reference to a value you own, once you've taken a mutable reference you can't use the original until you drop that mutable reference, once you drop that mutable reference you can use the original owned value again".</p>



<a name="182597103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597103">(Dec 04 2019 at 20:45)</a>:</h4>
<p>so I think that's not really how I'd say that (it's sort of a "beginners view" imo)</p>



<a name="182597162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597162">(Dec 04 2019 at 20:45)</a>:</h4>
<p>there's no difference between <code>Foo&lt;'a, T&gt;</code> and <code>&amp;mut T</code></p>



<a name="182597211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597211">(Dec 04 2019 at 20:46)</a>:</h4>
<p>(modulo GATs not being a thing)</p>



<a name="182597257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597257">(Dec 04 2019 at 20:46)</a>:</h4>
<p>I thought the difference was that you can have more than one <code>&amp;T</code> or <code>Foo&lt;'a, T&gt;</code> at a time, but not more than one <code>&amp;mut T</code>?</p>



<a name="182597304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597304">(Dec 04 2019 at 20:47)</a>:</h4>
<p>well, <code>&amp;mut T</code> is basically just an owned T</p>



<a name="182597314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597314">(Dec 04 2019 at 20:47)</a>:</h4>
<p>borrow check only cares about the lifetime, not the type</p>



<a name="182597429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597429">(Dec 04 2019 at 20:48)</a>:</h4>
<p>So what prevents having a <code>&amp;mut T</code> and a <code>&amp;T</code> to the same <code>T</code> at the same time?</p>



<a name="182597484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597484">(Dec 04 2019 at 20:48)</a>:</h4>
<p><em>Creating</em> a (shared or mutable) reference is a special operation that the borrow checker understands to create what's internally called a "loan" of the place (which can itself be shared or mutable/exclusive), but this loan is linked to a <em>lifetime</em> <code>'a</code> and thus to all values whose type contains <code>'a</code>, regardless of what that type is.</p>



<a name="182597667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597667">(Dec 04 2019 at 20:50)</a>:</h4>
<p>this is actually sort of why a signature like <code>fn(&amp;mut T) -&gt; &amp;U</code> is really painful, too</p>



<a name="182597674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597674">(Dec 04 2019 at 20:50)</a>:</h4>
<p>A nice resource for this might be <a href="https://nikomatsakis.github.io/rust-belt-rust-2019/" target="_blank" title="https://nikomatsakis.github.io/rust-belt-rust-2019/">https://nikomatsakis.github.io/rust-belt-rust-2019/</a></p>



<a name="182597683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597683">(Dec 04 2019 at 20:50)</a>:</h4>
<p>🤯</p>



<a name="182597684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597684">(Dec 04 2019 at 20:50)</a>:</h4>
<p>as borrow check doesn't know that you've "lost" the mutable ref</p>



<a name="182597699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597699">(Dec 04 2019 at 20:50)</a>:</h4>
<p>OK. So once you <em>have</em> a mutable/exclusive loan of the location, you can safely and without UB turn it into a <em>different</em> mutable/exclusive loan of the location?</p>



<a name="182597744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597744">(Dec 04 2019 at 20:51)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> Ah. So <code>fn foo(&amp;mut T) -&gt; &amp;T</code> kinda gives you a non-mut reference "backed" by an exclusive loan?</p>



<a name="182597753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597753">(Dec 04 2019 at 20:51)</a>:</h4>
<p>correct, so long as that "turn it into" is a move-like operation (i.e., happens once)</p>



<a name="182597755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597755">(Dec 04 2019 at 20:51)</a>:</h4>
<p>Yes</p>



<a name="182597790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597790">(Dec 04 2019 at 20:52)</a>:</h4>
<p>Does the compiler actually <em>enforce</em> that it's a move-like operation? Or do you just have to never touch self again once you've transmuted?</p>



<a name="182597791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597791">(Dec 04 2019 at 20:52)</a>:</h4>
<p><a href="https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self" target="_blank" title="https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self">https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self</a> may also be interesting for you to read (sort of tangentially related)</p>



<a name="182597869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597869">(Dec 04 2019 at 20:52)</a>:</h4>
<blockquote>
<p>Does the compiler actually <em>enforce</em> that it's a move-like operation? Or do you just have to never touch self again once you've transmuted?</p>
</blockquote>
<p>In the case, I showed, yes -- mem::transmute cannot be called more than once, you'll get a "use after move"</p>



<a name="182597895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597895">(Dec 04 2019 at 20:53)</a>:</h4>
<p>Ah, because you're "moving" <code>&amp;mut self</code>?</p>



<a name="182597901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597901">(Dec 04 2019 at 20:53)</a>:</h4>
<p>exactly</p>



<a name="182597902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597902">(Dec 04 2019 at 20:53)</a>:</h4>
<p>Same semantics as "consuming" any other owned value?</p>



<a name="182597903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597903">(Dec 04 2019 at 20:53)</a>:</h4>
<p><code>transmute</code> moves its argument and <code>&amp;mut T</code> isn't Copy so I <em>think</em> it should be fine? But I am never sure where reborrows can happen so <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="182597934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597934">(Dec 04 2019 at 20:53)</a>:</h4>
<p>So, wait a moment...</p>



<a name="182597981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182597981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182597981">(Dec 04 2019 at 20:53)</a>:</h4>
<p>to my knowledge we never reborrow in a function call that takes a non-reference type, but I may be wrong about that (e.g., around <code>impl AsRef</code>)</p>



<a name="182598033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598033">(Dec 04 2019 at 20:54)</a>:</h4>
<p>What happens if I write:</p>
<div class="codehilite"><pre><span></span>let x: &amp;mut T = &amp;mut some_t_I_own;
foo(x); // takes &amp;mut T
bar(x); // takes &amp;mut T
</pre></div>



<a name="182598050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598050">(Dec 04 2019 at 20:54)</a>:</h4>
<p>Does <code>bar(x)</code> fail to compile because <code>foo(x)</code> consumed <code>x</code>?</p>



<a name="182598064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598064">(Dec 04 2019 at 20:54)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> So we could have <code>u32::as_mut_le_bytes</code> and it would be entirely safe?</p>



<a name="182598110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598110">(Dec 04 2019 at 20:55)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> If that code works, then it's because it was reborrowed (i.e. <code>&amp;mut *x</code> was inserted) at the call sites</p>



<a name="182598128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598128">(Dec 04 2019 at 20:55)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> I don't think <code>as_mut_le_bytes</code> is implementable unless you're on an le platform. ;)</p>



<a name="182598137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598137">(Dec 04 2019 at 20:55)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> Ah.</p>



<a name="182598143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598143">(Dec 04 2019 at 20:55)</a>:</h4>
<p>OK then.</p>



<a name="182598155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598155">(Dec 04 2019 at 20:55)</a>:</h4>
<p>Well, I have a safe transmute proposal to rewrite then!</p>



<a name="182598159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598159">(Dec 04 2019 at 20:55)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> So we could have <code>u32::as_mut_le_bytes</code> and it would be entirely safe?</p>
</blockquote>
<p>Well, I mean, yes, but it's unlikely to be what you want</p>



<a name="182598169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598169">(Dec 04 2019 at 20:56)</a>:</h4>
<p>Thank you very much for the help!</p>



<a name="182598221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598221">(Dec 04 2019 at 20:56)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> Ah yes true, we could have <code>u32::as_mut_ne_bytes</code> though.</p>



<a name="182598247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598247">(Dec 04 2019 at 20:56)</a>:</h4>
<p>there's actually an issue to that effect, let me try to dig that up</p>



<a name="182598268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598268">(Dec 04 2019 at 20:56)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> I'm planning to spell that just <code>as_mut_bytes</code>. ;)</p>



<a name="182598287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598287">(Dec 04 2019 at 20:56)</a>:</h4>
<p>(or possibly <code>as_bytes_mut</code>)</p>



<a name="182598424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598424">(Dec 04 2019 at 20:58)</a>:</h4>
<p>I guess if there's only one method omitting <code>ne</code> would be fine. Though I would wonder if it's also possible to have it always work for a specific endianness?</p>



<a name="182598442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598442">(Dec 04 2019 at 20:58)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/64464" target="_blank" title="https://github.com/rust-lang/rust/issues/64464">https://github.com/rust-lang/rust/issues/64464</a></p>



<a name="182598929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598929">(Dec 04 2019 at 21:02)</a>:</h4>
<p>Is it possible to create a slice that goes in reverse of another slice?</p>



<a name="182598986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598986">(Dec 04 2019 at 21:03)</a>:</h4>
<div class="codehilite"><pre><span></span>| 0 | 1 | 2| 3 | 4 |
      &lt;--------
</pre></div>



<a name="182598999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182598999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182598999">(Dec 04 2019 at 21:03)</a>:</h4>
<blockquote>
<p>Hypothetical: </p>
</blockquote>
<p>What am I missing? <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e595e943b7fa16f4cce4e728603d5870" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e595e943b7fa16f4cce4e728603d5870">This just works</a>.</p>



<a name="182599023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599023">(Dec 04 2019 at 21:03)</a>:</h4>
<p>No -- that issue mentions as such, slices always go "forwards"</p>



<a name="182599098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599098">(Dec 04 2019 at 21:04)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> <span class="user-mention" data-user-id="239881">@Josh Triplett</span> was not aware/misunderstanding something, I think</p>



<a name="182599136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599136">(Dec 04 2019 at 21:04)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> No, slices have to go forwards.</p>



<a name="182599159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599159">(Dec 04 2019 at 21:05)</a>:</h4>
<p>Reading the whole thread. In C++ T&amp; aren't values, but in Rust, a <code>&amp;mut T</code> is just a normal value, and you can just move it around. Maybe that was causing the confusion?</p>



<a name="182599313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599313">(Dec 04 2019 at 21:06)</a>:</h4>
<p>I think the cause of confusion is thinking of references as more special than they are, not realizing the generality of lifetime variables. A common mistake sadly encouraged by almost all of our documentation.</p>



<a name="182599329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599329">(Dec 04 2019 at 21:06)</a>:</h4>
<p>I definitely didn't have any confusion with C++ references, since I'm not a C++ developer. ;)</p>



<a name="182599342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599342">(Dec 04 2019 at 21:06)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I didn't quite expect it <code>&amp;mut T</code> itself to have "move" semantics, and thus didn't realize this worked without mutable aliasing. I also didn't realize that a function from <code>&amp;mut self</code> to <code>&amp;mut U</code> would preserve the exclusivity of the borrow (or really <em>anything</em> about the borrow, if it used transmute).</p>



<a name="182599425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599425">(Dec 04 2019 at 21:07)</a>:</h4>
<p>I find useful to think of <code>&amp;mut T</code> as just <code>type MutRef = &amp;mut T;</code></p>



<a name="182599599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599599">(Dec 04 2019 at 21:08)</a>:</h4>
<p>That's definitely the semantic I <em>wanted</em>, but it somehow felt too good to be true that it actually worked that way.</p>



<a name="182599601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599601">(Dec 04 2019 at 21:08)</a>:</h4>
<p>If you mentally replace it everywhere with <code>MutRef</code>, it looks like just a normal type name, and same rules applies</p>



<a name="182599628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599628">(Dec 04 2019 at 21:08)</a>:</h4>
<p>(to be fully correct you need to add the life-time, but...)</p>



<a name="182599690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599690">(Dec 04 2019 at 21:09)</a>:</h4>
<p>FWIW I had this aha moment at some point as well</p>



<a name="182599734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599734">(Dec 04 2019 at 21:09)</a>:</h4>
<p>I don't know why but I also though that references were more special than they are (in C++ they are, and I came with a C++ background, so probably just extrapolated that to Rust).</p>



<a name="182599817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599817">(Dec 04 2019 at 21:10)</a>:</h4>
<p>Building on <span class="user-mention" data-user-id="132920">@gnzlbg</span>'s suggestion, I would describe <code>&amp;mut</code> as <code>struct MutRef&lt;'a, T&gt;(*mut T, PhantomInvariant&lt;'a&gt;);</code> with some syntactic sugar. Except we don't have <code>PhantomInvariant</code>. (Can it even be defined without involving <code>&amp;mut</code>?)</p>



<a name="182599928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599928">(Dec 04 2019 at 21:11)</a>:</h4>
<p>I think so -- via trait objects</p>



<a name="182599981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182599981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182599981">(Dec 04 2019 at 21:11)</a>:</h4>
<p>in particular IIRC <code>dyn Trait&lt;'a&gt;</code> is invariant over the <code>'a</code> because it could be <code>&amp;'a mut ()</code> internally or so</p>



<a name="182600079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600079">(Dec 04 2019 at 21:12)</a>:</h4>
<p>oh right</p>



<a name="182600189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600189">(Dec 04 2019 at 21:13)</a>:</h4>
<p>The confusion I had was that I assumed <code>&amp;mut T</code> being passed to a <code>&amp;mut T</code> parameter had something to do with <code>T</code> (or more to the point with the implicit lifetime <code>'a</code> in the <code>&amp;'a mut T</code>, and wasn't just completely opaque, because the compiler would need to track something something lifetimes.</p>



<a name="182600200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600200">(Dec 04 2019 at 21:13)</a>:</h4>
<p>This is a major improvement in my understanding. :)</p>



<a name="182600290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600290">(Dec 04 2019 at 21:14)</a>:</h4>
<p><code>PhantomData&lt;Cell&lt;&amp;'a ()&gt;&gt;</code> is also invariant over the <code>'a</code> IIRC</p>



<a name="182600364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600364">(Dec 04 2019 at 21:15)</a>:</h4>
<p>(I think it certainly <em>can</em> -- with re-borrowing -- but that's sort of handwavy since there's no good reference to my knowledge when reborrowing can or can't occur</p>



<a name="182600638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182600638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182600638">(Dec 04 2019 at 21:18)</a>:</h4>
<blockquote>
<p>PhantomData&lt;Cell&lt;&amp;'a ()&gt;&gt; is also invariant over the 'a IIRC</p>
</blockquote>
<p>Yes that's correct.</p>



<a name="182631244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182631244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182631244">(Dec 05 2019 at 06:23)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> for reference, <code>bytemuck</code> already lets you cast <code>&amp;mut T</code> and <code>&amp;mut [T]</code> between the Pod types.</p>



<a name="182636773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182636773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182636773">(Dec 05 2019 at 08:08)</a>:</h4>
<p>One other question on this, now that I understand mutable references a little better:<br>
Is it legal, and semantically correct, to consume a mutable reference, produce a non-mutable reference with the same loan, then transmute that to a mutable reference with the same loan, and return that?</p>



<a name="182636843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182636843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182636843">(Dec 05 2019 at 08:08)</a>:</h4>
<p>For instance, could I go from <code>&amp;mut T</code> to <code>&amp;T</code> to <code>&amp;U</code> to <code>&amp;mut U</code>, within a method that consumes a <code>&amp;mut T</code> and returns a <code>&amp;mut U</code>?</p>



<a name="182638327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638327">(Dec 05 2019 at 08:34)</a>:</h4>
<p>Not legal under stacked borrows I believe, or if there's a way to do it, it greatly depends on how exactly go from one reference type to another. But this has nothing to do with lifetimes, just with the principle that making a mutable reference from a shared one plays havoc with aliasing rules we want to have.</p>



<a name="182638464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638464">(Dec 05 2019 at 08:37)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> Oh, interesting. In that case, I have a follow-on question...</p>



<a name="182638509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638509">(Dec 05 2019 at 08:38)</a>:</h4>
<p>This is related to <a href="https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431/1" target="_blank" title="https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431/1">https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431/1</a></p>



<a name="182638556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638556">(Dec 05 2019 at 08:38)</a>:</h4>
<p>Suppose I have a trait that provides a function from <code>&amp;T</code> to <code>&amp;U</code>, which is a safe wrapper around <code>std::mem::transmute</code> with requirements that ensure it's safe.</p>



<a name="182638567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638567">(Dec 05 2019 at 08:38)</a>:</h4>
<p>Suppose I also have traits that provide safe functions from <code>&amp;T</code> to and from byte slices.</p>



<a name="182638627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638627">(Dec 05 2019 at 08:39)</a>:</h4>
<p>I'd like to have this function (inside the <code>ToBytes</code> trait):</p>
<div class="codehilite"><pre><span></span><span class="w">    </span><span class="k">fn</span> <span class="nf">cast_mut</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">FromBytes</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">FromBytesError</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">      </span><span class="k">where</span><span class="w"> </span><span class="n">Self</span>: <span class="nc">FromAnyBytes</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/*...*/</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="182638695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638695">(Dec 05 2019 at 08:40)</a>:</h4>
<p>The <code>FromBytes</code> trait provides a method from <code>&amp;[u8]</code> to <code>&amp;T</code>.</p>



<a name="182638715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638715">(Dec 05 2019 at 08:40)</a>:</h4>
<p>It doesn't provide a method to get a <code>&amp;mut T</code> because that's not safe without additional guarantees (namely those enforced by <code>cast_mut</code> above).</p>



<a name="182638818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638818">(Dec 05 2019 at 08:42)</a>:</h4>
<p>Does that make that function un-implementable, even though it otherwise has all the requirements it needs to do so safely?</p>



<a name="182638958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182638958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182638958">(Dec 05 2019 at 08:44)</a>:</h4>
<p>It would be possible to have <code>cast_mut</code> transmute directly from <code>&amp;mut Self</code> to <code>&amp;mut T</code> without going via <code>T::from_bytes</code>, but that would require <code>T: FromAnyBytes</code> rather than <code>T: FromBytes</code>, which would exclude types like <code>bool</code> that need to validate the bytes first.</p>



<a name="182639479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639479">(Dec 05 2019 at 08:54)</a>:</h4>
<p>Couldn't you downgrade the &amp;mut Self to &amp;Self, try the FromBytes cast, and if it succeeds do the transmute from self to &amp;mut T directly, ignoring the &amp;T you got from FromBytes?</p>



<a name="182639517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639517">(Dec 05 2019 at 08:55)</a>:</h4>
<p>Alternatively, have FromBytes provide the validation separately from the cast (presumably the cast could then be a provided method)</p>



<a name="182639525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639525">(Dec 05 2019 at 08:55)</a>:</h4>
<p>...we could, yes. That seems <em>wasteful</em>, but that would work.</p>



<a name="182639543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639543">(Dec 05 2019 at 08:55)</a>:</h4>
<p>I guess if it gets inlined the waste won't actually matter.</p>



<a name="182639827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639827">(Dec 05 2019 at 09:00)</a>:</h4>
<p>And factoring out the validation seems nice, too.</p>



<a name="182639902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639902">(Dec 05 2019 at 09:00)</a>:</h4>
<p>Is there a way to have a trait method that you <em>can't</em> override, and can only take the default implementation of? Or would that require having a sealed trait that's implemented for the unsealed trait?</p>



<a name="182639977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182639977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182639977">(Dec 05 2019 at 09:01)</a>:</h4>
<p>Josh I think your whole plan is fundamentally a bad path</p>



<a name="182640043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640043">(Dec 05 2019 at 09:02)</a>:</h4>
<p>casting should always be cheap</p>



<a name="182640050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640050">(Dec 05 2019 at 09:02)</a>:</h4>
<p>if you let user logic in that's a bad time</p>



<a name="182640266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640266">(Dec 05 2019 at 09:05)</a>:</h4>
<p><code>core::mem::transmute</code> is unsafe because it's too permissive, but it's fundamentally the correct design from an API usage perspective</p>



<a name="182640364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640364">(Dec 05 2019 at 09:07)</a>:</h4>
<p>unfortunately it's 2am my zone, so if you want more you'll have to catch me again some time</p>



<a name="182640709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640709">(Dec 05 2019 at 09:13)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> Casting <em>should</em> always be cheap, yes. But, for instance, if the options are "you can't safely cast a byte to bool" or "you can cast a byte to bool with a single check to make sure the byte is 0 or 1", I'll take the latter.</p>



<a name="182640774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640774">(Dec 05 2019 at 09:14)</a>:</h4>
<p>Casting from bytes to <code>u32</code> should optimize down to no code, because any possible bytes are a valid <code>u32</code>.</p>



<a name="182640782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182640782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182640782">(Dec 05 2019 at 09:14)</a>:</h4>
<p>Casting from bytes to <code>bool</code> has to either validate or be unsafe.</p>



<a name="182641023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182641023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182641023">(Dec 05 2019 at 09:18)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> It's 1am my time as well, and I'm going to sleep too. We can talk more another time; I'd very much like to get your feedback.</p>



<a name="182642624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182642624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182642624">(Dec 05 2019 at 09:43)</a>:</h4>
<blockquote>
<p>For instance, could I go from &amp;mut T to &amp;T to &amp;U to &amp;mut U, within a method that consumes a &amp;mut T and returns a &amp;mut U?</p>
</blockquote>
<p>You can't go from <code>&amp;</code> to <code>&amp;mut</code>, but you can go from <code>&amp;mut -&gt; usize  (or (usize,usize))-&gt; &amp;mut</code>.</p>



<a name="182642723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182642723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182642723">(Dec 05 2019 at 09:44)</a>:</h4>
<blockquote>
<p>Casting from bytes to u32 should optimize down to no code, because any possible bytes are a valid u32.</p>
</blockquote>
<p>Is that so? You can't safely cast a <code>MaybeUninit&lt;u32&gt;</code> to an <code>u32</code> (or at least, not today), because not all bitpatterns that the storage of an u32 can take are valid for u32.</p>



<a name="182642891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182642891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182642891">(Dec 05 2019 at 09:46)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> Yes, that's so. These traits specifically support casting from <code>&amp;[u8]</code>, which is necessarily already initialized.</p>



<a name="182642940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182642940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182642940">(Dec 05 2019 at 09:47)</a>:</h4>
<p>Also, yes, all bit patterns that the storage of a <code>u32</code> can take are valid for <code>u32</code>. The problem with <code>MaybeUninit</code> is the UB of reading uninitialized memory. That doesn't apply to <code>[u8]</code>.</p>



<a name="182642990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182642990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182642990">(Dec 05 2019 at 09:48)</a>:</h4>
<blockquote>
<p>Also, yes, all bit patterns that the storage of a u32 can take are valid for u32.</p>
</blockquote>
<p>This is wrong, or at least, this is not the case today. For example, the 0xUU bitpattern isn't valid for u32.</p>



<a name="182643006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643006">(Dec 05 2019 at 09:48)</a>:</h4>
<p>I'm talking about real machines with real bits. :P</p>



<a name="182643014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643014">(Dec 05 2019 at 09:48)</a>:</h4>
<p>Ah, I was talking about Rust.</p>



<a name="182643031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643031">(Dec 05 2019 at 09:49)</a>:</h4>
<p>Not the LLVM abstract model and the concept of "undef", which, again, <em>doesn't apply to <code>[u8]</code></em>.</p>



<a name="182643056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643056">(Dec 05 2019 at 09:49)</a>:</h4>
<p>I didn't say anything about LLVM.</p>



<a name="182643105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643105">(Dec 05 2019 at 09:50)</a>:</h4>
<p>If you have a <code>[u8]</code> and it points to unitialized memory, you've <em>already</em> done something wrong. The operation from <code>&amp;[u8]</code> to <code>&amp;u32</code> doesn't do anything wrong.</p>



<a name="182643127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643127">(Dec 05 2019 at 09:50)</a>:</h4>
<p>0xUU isn't a bit pattern.</p>



<a name="182643139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643139">(Dec 05 2019 at 09:50)</a>:</h4>
<blockquote>
<p>If you have a [u8] and it points to unitialized memory, you've already done something wrong. The operation from &amp;[u8] to &amp;u32 doesn't do anything wrong.</p>
</blockquote>
<p>That's correct.</p>



<a name="182643182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643182">(Dec 05 2019 at 09:51)</a>:</h4>
<blockquote>
<p>0xUU isn't a bit pattern.</p>
</blockquote>
<p>What's the bitpattern of <code>MaybeUninit::&lt;u8&gt;::uninit()</code> ?</p>



<a name="182643261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643261">(Dec 05 2019 at 09:52)</a>:</h4>
<p>Something in the range 0x00 to 0xFF and you don't know what.</p>



<a name="182643278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643278">(Dec 05 2019 at 09:52)</a>:</h4>
<p>I'm not (today) attempting to have an argument about the UB of reading uninitialized memory.</p>



<a name="182643306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643306">(Dec 05 2019 at 09:53)</a>:</h4>
<blockquote>
<p>Something in the range 0x00 to 0xFF and you don't know what.</p>
</blockquote>
<p>This is incorrect.</p>



<a name="182643408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643408">(Dec 05 2019 at 09:54)</a>:</h4>
<p>Please don't attempt to define the words I'm using. Feel free to define words you're using.</p>



<a name="182643412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643412">(Dec 05 2019 at 09:54)</a>:</h4>
<p>Let's try this again. It's 1:53am. I have <em>zero</em> interest in hearing yet another treatise on UB.</p>



<a name="182643445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643445">(Dec 05 2019 at 09:55)</a>:</h4>
<p>I use the rust abstract machine definitions, which defines all rust bits as being 0, 1, or U.</p>



<a name="182643446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643446">(Dec 05 2019 at 09:55)</a>:</h4>
<p>I'm talking about hardware, and bit patterns that can <em>actually appear in memory</em>, and a cast operation that goes from <code>&amp;[u8]</code> to <code>&amp;T</code>. So I don't have to care about uninitialized memory, thankfully.</p>



<a name="182643548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643548">(Dec 05 2019 at 09:56)</a>:</h4>
<p>Please take it to a thread that's about uninitialized memory. Not everyone talking about bit patterns in actual memory is talking about an abstract machine.</p>



<a name="182643649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643649">(Dec 05 2019 at 09:56)</a>:</h4>
<p>Your assumption that <code>MaybeUninit::&lt;u8&gt;::uninit()</code> is a bit pattern in range 0x00 and 0xFF is wrong, and would mean that:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span>::<span class="n">uninit</span><span class="p">().</span><span class="n">assume_init</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</pre></div>


<p>is a safe operation, and that </p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span>::<span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span>::<span class="n">uninit</span><span class="p">();</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">x</span><span class="p">.</span><span class="n">assume_init</span><span class="p">(),</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">assume_init</span><span class="p">())</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</pre></div>


<p>never panics. Yet the <code>.assume_init()</code> is UB, and anything can happen.</p>



<a name="182643919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643919">(Dec 05 2019 at 10:00)</a>:</h4>
<p>Please stop trying to redefine the terms I'm using, and please stop assuming I don't understand Rust just because I'm not using the terms you prefer.</p>



<a name="182643957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182643957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182643957">(Dec 05 2019 at 10:01)</a>:</h4>
<p>Bytes in memory always have a value. The Rust abstract machine defines the behavior of the compiler, not the hardware. And for the purposes of this thread, I don't <em>care</em> about uninitialized memory because I'm dealing with <code>[u8]</code>.</p>



<a name="182645371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182645371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182645371">(Dec 05 2019 at 10:22)</a>:</h4>
<p><a href="#narrow/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation/near/182642891" title="#narrow/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation/near/182642891">I agreed with you on the first line of this conversation that for <code>[u8]</code> there is no problem</a>.  Yet you keep repeating that all bitpatterns are valid for <code>u32</code>, which I think is incorrect for the purpose of designing a safe transmute trait which appears to be your goal.</p>



<a name="182645521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182645521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182645521">(Dec 05 2019 at 10:24)</a>:</h4>
<blockquote>
<p>I'm not using the terms you prefer.</p>
</blockquote>
<p>I'm using the terms most people talking about this have agreed on and that we have in writing. If everybody defines these terms to mean something differently, communication becomes really hard.</p>



<a name="182647910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182647910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182647910">(Dec 05 2019 at 10:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> wrote:</p>
<blockquote>
<p>there's no difference between <code>Foo&lt;'a, T&gt;</code> and <code>&amp;mut T</code></p>
</blockquote>
<p>by the way: This is not quite true. Namely, <code>Foo</code> may be <em>covariant</em> or <em>invariant</em> with respect to  <code>T</code>, while <code>&amp;'a mut T</code> is always invariant with respect to <code>T</code>.  (The variance of <code>Foo</code> is based inferred on the ADT-definition of <code>Foo</code>; there's currently no way to declare variance explicitly.)</p>



<a name="182663205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182663205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182663205">(Dec 05 2019 at 14:23)</a>:</h4>
<p>(See <a href="https://doc.rust-lang.org/nightly/nomicon/phantom-data.html" target="_blank" title="https://doc.rust-lang.org/nightly/nomicon/phantom-data.html">https://doc.rust-lang.org/nightly/nomicon/phantom-data.html</a> for variances)</p>



<a name="182663347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182663347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182663347">(Dec 05 2019 at 14:24)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> in case you haven read it, <span class="user-mention" data-user-id="120791">@RalfJ</span>'s <a href="https://www.ralfj.de/blog/2019/07/14/uninit.html" target="_blank" title="https://www.ralfj.de/blog/2019/07/14/uninit.html">https://www.ralfj.de/blog/2019/07/14/uninit.html</a> is a good read</p>



<a name="182663886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182663886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182663886">(Dec 05 2019 at 14:31)</a>:</h4>
<p>Also relevant:<br>
- <a href="https://arxiv.org/abs/1903.00982" target="_blank" title="https://arxiv.org/abs/1903.00982">https://arxiv.org/abs/1903.00982</a><br>
- <a href="https://aaronweiss.us/pubs/popl19-src-oxide-slides.pdf" target="_blank" title="https://aaronweiss.us/pubs/popl19-src-oxide-slides.pdf">https://aaronweiss.us/pubs/popl19-src-oxide-slides.pdf</a></p>



<a name="182706002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182706002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182706002">(Dec 05 2019 at 21:25)</a>:</h4>
<p>I don't think that safe transmuting uninit to init was ever on the table, or being proposed.<br>
In the absence of a freeze intrinsic, that's clearly abstract machine UB.<br>
We are pre-supposing that you have initialized memory in one type and want it in a new type.<br>
The problem of uninitialized memory is an entirely different beast to attack, in some other topic elsewhere.</p>



<a name="182710072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182710072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182710072">(Dec 05 2019 at 22:07)</a>:</h4>
<p>That said, accidentally reading uninit memory <em>is a valid concern</em>, which absolutely limits which types can be part of any particular safe-casting framework, for example <code>#[repr(C)]struct Ex {a: u16, b:u8}</code> <strong>can't</strong> be safe cast to <code>[u8;4]</code> even though the size/align would otherwise allow it, because you'd be exposing an uninit byte in the index 3 position of the output array.</p>



<a name="182710967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182710967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182710967">(Dec 05 2019 at 22:17)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> That's why the <code>ToBytes</code> type we proposed doesn't allow implementations for that type, because it contains padding (at the end).</p>



<a name="182711034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182711034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182711034">(Dec 05 2019 at 22:18)</a>:</h4>
<p>We're carefully defining the semantics to <em>avoid</em> reading uninitialized memory, rather than attempting to define semantics <em>for</em> reading uninitialized memory.</p>



<a name="182715461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182715461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182715461">(Dec 05 2019 at 23:27)</a>:</h4>
<p>Right, that was an, "in gnzlbg's defense they've partly got a point" sort of note ;3</p>



<a name="182836444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836444">(Dec 07 2019 at 09:10)</a>:</h4>
<blockquote>
<p>Not the LLVM abstract model and the concept of "undef", which, again, <em>doesn't apply to <code>[u8]</code></em>.</p>
</blockquote>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span>  of course it does? I do not understand why you bring up <code>u8</code> as if it was a special case. It is not.</p>
<blockquote>
<p>I'm talking about real machines with real bits. :P</p>
</blockquote>
<p>So you are writing assembly code then? Because if you are writing Rust, real machines don't matter. (Well yeah I know I am exaggerating but it's not entirely wrong either.)</p>



<a name="182836464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836464">(Dec 07 2019 at 09:11)</a>:</h4>
<blockquote>
<p>Bytes in memory always have a value. The Rust abstract machine defines the behavior of the compiler, not the hardware.</p>
</blockquote>
<p>True. But as long as you are writing Rust code, it is fundamentally impossible to use hardware-level concepts to reason about your code.</p>



<a name="182836467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836467">(Dec 07 2019 at 09:11)</a>:</h4>
<p>So if you want to make an argument about how your Rust code behaves, the <em>only option</em> you have is the Rust Abstract machine. A safe function that transmutes <code>MaybeUninit&lt;u8&gt;</code> to <code>u8</code> is unsound, can cause UB on the Rust Abstract Machine, and can lead to real bugs in the compiled program. It is of upmost importance that a safe transmute proposal avoids such transmutes.</p>



<a name="182836468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836468">(Dec 07 2019 at 09:11)</a>:</h4>
<blockquote>
<p><a href="https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self" target="_blank" title="https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self">https://github.com/rust-embedded/wg/blob/master/rfcs/0377-mutex-trait.md#the-rust-standard-library-mutex-uses-self-for-the-lock-method-why-choose-mut-self</a> may also be interesting for you to read (sort of tangentially related)</p>
</blockquote>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span>  that is <em>really weird</em>... why would one ever use <code>Mutex&lt;RefCell&lt;T&gt;&gt;</code>? That's two layers of interior mutability...<br>
Not sure I buy the deadlock-safety argument; we can still create deadlocks through inconsistent lock ordering.<br>
And then I can also still reentrantly acquire the same mutex with their <code>&amp;mut &amp;Mutex</code> scheme so how does it even help...?</p>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> </p>
<blockquote>
<p>Except we don't have PhantomInvariant. (Can it even be defined without involving &amp;mut?)</p>
</blockquote>
<p>Alternative def.n: <code>type PhantomInvariant&lt;'a&gt; = fn(&amp;'a ()) -&gt; &amp;'a ()</code></p>



<a name="182836593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836593">(Dec 07 2019 at 09:14)</a>:</h4>
<p>First of all, my point was that once you have a &amp;[u8] (or some other type that isn't MaybeUninit) you have real memory that can't be uninitialized (or you've done something undefined).</p>
<p>Second, I'm extremely done with this thread, with the "Rust Abstract Machine", and with formal methods in general.</p>



<a name="182836609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836609">(Dec 07 2019 at 09:15)</a>:</h4>
<blockquote>
<p>Second, I'm extremely done with this thread, with the "Rust Abstract Machine", and with formal methods in general.</p>
</blockquote>
<p>Well that's not very helpful. LLVM (and every C compiler) is full of soundness bugs because of such an attitude. I hope we can avoid Rust repeating all the same mistakes.</p>



<a name="182836671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836671">(Dec 07 2019 at 09:17)</a>:</h4>
<blockquote>
<p>First of all, my point was that once you have a &amp;[u8] (or some other type that isn't MaybeUninit) you have real memory that can't be uninitialized (or you've done something undefined).</p>
</blockquote>
<p>Such an argument can be made on the R-AM just fine, and I agree for the case of <code>u8</code>. Not sure what you mean by "real", but the safety invariant of <code>u8</code> rules out uninitialized bit patterns. However that's not true for every type; e.g. <code>(u8, u16)</code> does contain an uninitialized byte and thus a transmute from <code>(u8, u16)</code> to <code>[u8; 4]</code> is UB.</p>



<a name="182836722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836722">(Dec 07 2019 at 09:18)</a>:</h4>
<p>(This is assuming our current conservative rules, see <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/71" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/71">https://github.com/rust-lang/unsafe-code-guidelines/issues/71</a>. But even if that transmute is not UB any more one day it is still unsafe.)</p>



<a name="182836878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182836878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182836878">(Dec 07 2019 at 09:23)</a>:</h4>
<p>I feel like we're retreading previous territory. I'm aware that padding is an issue, the proposal in the thread I previously linked to goes to great lengths to address that.</p>



<a name="182837116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182837116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182837116">(Dec 07 2019 at 09:29)</a>:</h4>
<blockquote>
<p>I feel like we're retreading previous territory. I'm aware that padding is an issue, the proposal in the thread I previously linked to goes to great lengths to address that.</p>
</blockquote>
<p>Great! Sorry for not having read up on the entire context. I was mostly reacting to individual statements here that (at least without context) are thoroughly incompatible with the UCG's view of this -- in particular the part about "real machines" and "<code>0xUU</code> isnt a bit pattern". Some of this is likely "just" temrinology, but then figuring out terminology is a big part of formally defining anything.<br>
I misunderstood what you mean by "undef doesnt apply to [u8]"; sorry for that.</p>



<a name="182837490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Taking%20mutable%20references%20via%20programmatic%20operation/near/182837490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Taking.20mutable.20references.20via.20programmatic.20operation.html#182837490">(Dec 07 2019 at 09:39)</a>:</h4>
<p>It's alright. I'm a bit frustrated at the moment and it isn't your fault. I'm not dismissing formal methods, I just don't always want to have every discussion about code behavior forced into formal methods.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>